﻿    using Mota.XMLControls;
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;

namespace Mota.EditModule
{
    public class EditFormCollectDataVisitor:GuiVisitor
    {
        private static string keyName = "XMLAttribute";
        private Dictionary<string,object> dataList = null;
        private BindingSource binding = null;
        public Dictionary<string,object> CollectedList
        {
            get
            {
                return this.dataList;
            }
        }
        //TODO: add dispose method
        public EditFormCollectDataVisitor(BindingSource binding)
        {
            this.dataList = new Dictionary<string,object>();
            this.binding = binding;
        }
       
        public override void Visit(GuiComboBox guiCombobox)
        {
            GetDataFrom(guiCombobox);
        }

        public override void Visit(GuiTextBox guiTextBox)
        {
            GetDataFrom(guiTextBox);
        }

        public override void Visit(GuiRichText guiRichText)
        {
            GetDataFrom(guiRichText);
        }
        
        public override void Visit(GuiDateTime guiDateTime)
        {
            GetDataFrom(guiDateTime);
        }

        public override void Visit(GuiImageBox guiImageBox)
        {
            GetDataFrom(guiImageBox);
            
        }

        public override void Visit(GuiCheckBox guiCheckBox)
        {
            GetDataFrom(guiCheckBox);
        }

        public override void Visit(GuiMaskText guiMaskText)
        {
            GetDataFrom(guiMaskText);
        }

        public override void Visit(GuiNumeric guiNumeric)
        {
            GetDataFrom(guiNumeric);
        }
        
        public override void Visit(GuiGroupBox guiGroupBox)
        {
        }

        public override void Visit(GuiLabel guiLabel)
        {
        }

        public override void Visit(GuiSplitter guiSplitter)
        {
        }

        public override void Visit(GuiSplitterPanel guiSplitterPanel)
        {
        }

        public override void Visit(GuiTab guiTab)
        {
        }

        public override void Visit(GuiTabControl guiTabControl)
        {
        }

        public override void Visit(GuiWebBrowser guiWebBrowser)
        {
        }

        public override void Visit(GuiCheckedList guiCheckedList)
        {
            GetMultiDataFrom(guiCheckedList);
        }

        public override void Visit(GuiTreeView guiTreeView)
        {
            GetDataFrom(guiTreeView);
        }

        public override void Visit(GuiTime guiTime)
        {
            GetDataFrom(guiTime);
        }

        public override void Visit(GuiColor guiColor)
        {
            GetDataFrom(guiColor);
        }

        public override void Visit(GuiTrackBar guiTrackBar)
        {
            GetDataFrom(guiTrackBar);
        }

        public override void Visit(GuiPanel guiPanel)
        {
        }

        public override void Visit(GuiRangeTrackBar guiRangeTrackBar)
        {
            GetMultiDataFrom(guiRangeTrackBar);
        }
        public override void Visit(GuiCheckedComboBox guiCheckedComboBox)
        {
            GetMultiDataFrom(guiCheckedComboBox);
        }

        public override void Visit(GuiListBox guiListBox)
        {
            GetDataFrom(guiListBox);
        }

        public override void Visit(GuiRadioGroup guiRadioGroup)
        {
            GetDataFrom(guiRadioGroup);
        }

        private void GetDataFrom(GuiControl ctrl)
        {
            if (ctrl.XmlAttributes.ContainsKey(keyName))
            {
                object value = null;
                if (!((binding.Current as DataRowView)[ctrl.XmlAttributes[keyName]] is DBNull))
                    value = (binding.Current as DataRowView)[ctrl.XmlAttributes[keyName]];
                dataList.Add(key: ctrl.XmlAttributes[keyName], value: value);
            }
        }
        private void GetMultiDataFrom(GuiControl ctrl)
        {
            if (ctrl.XmlAttributes.ContainsKey(keyName))
            {
                object value = null;
                if (!((binding.Current as DataRowView)[ctrl.XmlAttributes[keyName]] is DBNull))
                {
                    value = (binding.Current as DataRowView)[ctrl.XmlAttributes[keyName]];
                    dataList.Add(key: ctrl.XmlAttributes[keyName], value: XElement.Parse(value.ToString()));
                }
            }
        }

    }
}
